package com.hsyt.pms.sys.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pms.api.dto.system.SysUserDto;
import com.hsyt.pms.api.enums.Able;
import com.hsyt.pms.api.enums.Status;
import com.hsyt.pms.api.vo.system.SysUserVo;
import com.hsyt.pms.orm.sys.SysUserDao;
import com.hsyt.pms.sys.dubbo.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: SysUserServiceImpl
 * @Description: 用户服务实现类
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
@Slf4j
@Component
@Service(version = "0.0.1", interfaceClass = SysUserService.class, registry = "pms", group = "pmsSystem", timeout = 3000)
public class SysUserServiceImpl implements SysUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Resource
    private SysUserDao pmsUserDao;

    public void updateUserByKeySelective(SysUserDto pmsUserDto) {
        pmsUserDao.updateByPrimaryKeySelective(pmsUserDto);
    }

    @Override
    public SysUserVo selectUserByKey(Long userId) {
        SysUserVo vo = pmsUserDao.selectByPrimaryKey(userId);
        if (vo == null) {
            vo = new SysUserVo();
        }
        vo.setId(vo.getUserId());
        return vo;
    }

    @Override
    public void saveAdmin(SysUserDto dto) throws Exception {
        dto.setStatus(Status.ENABLED.getCode());
        dto.setEnabled(Able.ENABLED.getCode());
        if (dto.getId() == null) {
            if (pmsUserDao.countUserCode(dto.getUserName()) > 0) {
                pmsUserDao.updateByPrimaryKeySelective(dto);
                return;
            }
            pmsUserDao.insertSelective(dto);
        } else {
            pmsUserDao.updateByPrimaryKeySelective(dto);
        }
        if (dto.getRoleId() != null && dto.getId() != null) {
            pmsUserDao.deleteRole(dto.getId());
            pmsUserDao.insertRole(dto);
        }
    }

    @Override
    public List<SysUserVo> listYunAdmin(SysUserDto admin) {
        return pmsUserDao.listYunAdmin(admin);
    }

    @Override
    public PageResult<SysUserVo> searchPageDataByDto(SysUserDto dto) {
        Page<?> page = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize(), true);
        List<SysUserVo> list = pmsUserDao.queryUserByDto(dto);
        PageResult<SysUserVo> pageResult = PageResult.create(page.getTotal(), list);
        return pageResult;
    }

    @Override
    public void saveUserRole(Long userId, List<Long> roleIds) throws Exception {
        if (CollectionUtils.isNotEmpty(roleIds)) {
            for (int i = 0; i < roleIds.size(); i++) {
                if (roleIds.get(i) != null) {
                    pmsUserDao.insertUserRole(userId, roleIds.get(i));
                }
            }
        }
    }

    @Override
    public List<Long> queryUserRole(Long userId) throws Exception {
        return pmsUserDao.queryRole(userId);
    }

}
